package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MarketAddress;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.zhangshizhi.wx.address.AddressListVO;
import com.cskaoyan.service.AddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author little ants
 * @since 2022/06/29 19:58
 */

@RestController
@RequestMapping("wx/address")
public class AddressController {
    @Autowired
    AddressService service;

    /**
     * 获取该用户所有地址
     *
     * @return com.cskaoyan.bean.BaseRespVo
     * @author little ants
     * @since 2022/06/29 21:11
     */
    @RequestMapping("list")
    public BaseRespVo addressList() {

        Subject subject = SecurityUtils.getSubject();
        MarketUser principal = (MarketUser) subject.getPrincipal();
        AddressListVO addressLlistVO = service.getAddressList(principal.getId());


        // AddressListVO addressLlistVO = service.getAddressList(1);
        return new BaseRespVo().ok(addressLlistVO);
    }

    /**
     * 获取该用户某地址详细信息
     *
     * @return com.cskaoyan.bean.BaseRespVo
     * @author little ants
     * @since 2022/06/29 21:11
     */
    @RequestMapping("detail")
    public BaseRespVo addressDetail(Integer id) {
        MarketAddress marketAddress = service.getAddress(id);

        return new BaseRespVo().ok(marketAddress);
    }

    /**
     * 新增地址
     *
     * @param marketAddress
     * @return com.cskaoyan.bean.BaseRespVo
     * @author little ants
     * @since 2022/06/29 21:12
     */
    @RequestMapping("save")
    public BaseRespVo addressSave(@RequestBody MarketAddress marketAddress) {


        Subject subject = SecurityUtils.getSubject();
        MarketUser principal = (MarketUser) subject.getPrincipal();

        marketAddress.setUserId(principal.getId());
        service.saveAddress(marketAddress);
        return new BaseRespVo().ok();
    }

    /**
     * 删除该用户某个地址
     *
     * @param marketAddress
     * @return com.cskaoyan.bean.BaseRespVo
     * @author little ants
     * @since 2022/06/29 21:12
     */
    @RequestMapping("delete")
    public BaseRespVo addressDelete(@RequestBody MarketAddress marketAddress) {
        service.removeAddress(marketAddress);
        return new BaseRespVo().ok();
    }


}
